﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using System.Windows.Data;
using bdnt_pets_system_Entity;
using bdnt_pets_system.FailureServiceReference;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.FailuresTraking
{
    public partial class FailureTrackingManagerControl : BaseUserControl
    {
        FailureServiceReference.FailureServiceClient faiClient;
        int pageSize = SessionManager.PageSize;
        int currentPage = 1;

        //默认查询条件
        FRTManagerControlCondition selectFrt = new FRTManagerControlCondition()
        {
            PartName = MyStrings.qsrbjmc,// "请输入部件编号",
            FailureName = MyStrings.qsrgz // "请输入故障"
        };
        FRTManagerControlCondition CuFrt
        {
            get { return sConditon.DataContext as FRTManagerControlCondition; }
            set { sConditon.DataContext = value; }
        }

        public FailureTrackingManagerControl()
        {
            InitializeComponent();
            faiClient = new FailureServiceReference.FailureServiceClient();
            InitData();
            QueryData();
        }

        void InitData()
        {
            InitEvent();

            List<SimpleObject<int, string>> cuObjs = new List<SimpleObject<int, string>>() { 
                new SimpleObject<int, string>(){Item1=SessionManager.CurrentUser.Id, Item2=MyStrings.wdrw },
                new SimpleObject<int, string>(){Item1=0, Item2= MyStrings.syrw }
            };
            cbMyTraking.ItemsSource = cuObjs;
            cbMyTraking.DisplayMemberPath = "Item2";
            cbMyTraking.SelectedValuePath = "Item1";
            cbMyTraking.SelectedIndex = 0;

            tbPartName.Foreground = tbFailureName.Foreground = new SolidColorBrush(Colors.Gray);
            tbPartName.SetBinding(TextBox.TextProperty, new Binding("PartName") { Mode = BindingMode.TwoWay });
            tbPartName.Tag = selectFrt.PartName;

            tbFailureName.SetBinding(TextBox.TextProperty, new Binding("FailureName") { Mode = BindingMode.TwoWay });
            tbFailureName.Tag = selectFrt.FailureName;

            CuFrt = new FRTManagerControlCondition()
            {
                PartName = selectFrt.PartName,
                FailureName = selectFrt.FailureName
            };
        }

        void InitEvent()
        {
            faiClient.GetFailureTrackingListByConditionCountCompleted += new EventHandler<FailureServiceReference.GetFailureTrackingListByConditionCountCompletedEventArgs>(faiClient_GetFailureTrackingListByConditionCountCompleted);
            faiClient.GetFailureTrackingListByConditionCompleted += new EventHandler<FailureServiceReference.GetFailureTrackingListByConditionCompletedEventArgs>(faiClient_GetFailureTrackingListByConditionCompleted);
            failurePageC.dpPage.PageIndexChanged += new EventHandler<EventArgs>(dpPage_PageIndexChanged);
            btnSelect.Click += new RoutedEventHandler(btnSelect_Click);
            btnNew.Click += new RoutedEventHandler(btnNew_Click);
            btnBack.Click += new RoutedEventHandler(btnBack_Click);
            faiClient.OverTrackingCompleted += new EventHandler<OverTrackingCompletedEventArgs>(faiClient_OverTrackingCompleted);
            faiClient.DeleteTrackingCompleted += new EventHandler<DeleteTrackingCompletedEventArgs>(faiClient_DeleteTrackingCompleted);
        }

        void faiClient_DeleteTrackingCompleted(object sender, DeleteTrackingCompletedEventArgs e)
        {
            if (e.Result)
            {
                QueryData();
            }
        }

        void btnBack_Click(object sender, RoutedEventArgs e)
        {
            App.ToPrePage(this);
        }

        void faiClient_OverTrackingCompleted(object sender, OverTrackingCompletedEventArgs e)
        {
            if (e.Result)
            {
                QueryData();
            }
        }

        void btnNew_Click(object sender, RoutedEventArgs e)
        {
            App.ToPage(new NewTrakingControl());
        }

        void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            QueryData();
        }
        int cuEmpId = 0;
        void QueryData()
        {
            if (cbMyTraking.SelectedValue != null)
                cuEmpId = Int32.Parse(cbMyTraking.SelectedValue.ToString());

            faiClient.GetFailureTrackingListByConditionCountAsync(cuEmpId,
                CuFrt.PartName == selectFrt.PartName ? "" : CuFrt.PartName,
                  CuFrt.FailureName == selectFrt.FailureName ? "" : CuFrt.FailureName,
                  dpBDate.SelectedDate, dpEDate.SelectedDate);
        }

        void faiClient_GetFailureTrackingListByConditionCompleted(object sender, FailureServiceReference.GetFailureTrackingListByConditionCompletedEventArgs e)
        {
            gdList.ItemsSource = e.Result;
        }

        void faiClient_GetFailureTrackingListByConditionCountCompleted(object sender, FailureServiceReference.GetFailureTrackingListByConditionCountCompletedEventArgs e)
        {
            failurePageC.LoadPages(e.Result, pageSize);
        }
        void dpPage_PageIndexChanged(object sender, EventArgs e)
        {
            currentPage = failurePageC.PageIndex + 1;
            faiClient.GetFailureTrackingListByConditionAsync(cuEmpId,
                CuFrt.PartName == selectFrt.PartName ? "" : CuFrt.PartName,
                  CuFrt.FailureName == selectFrt.FailureName ? "" : CuFrt.FailureName,
                  dpBDate.SelectedDate, dpEDate.SelectedDate, pageSize, currentPage);
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            TrakingDescrWindow tdw = new TrakingDescrWindow((sender as HyperlinkButton).Tag as Tbl_FailureTracking);
            tdw.Show();
            tdw.Closed += new EventHandler(tdw_Closed);
        }

        void tdw_Closed(object sender, EventArgs e)
        {
            var cuo = sender as TrakingDescrWindow;
            if (cuo.DialogResult == true)
                QueryData();
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(MyStrings.qdyjszz, MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            var cuo = (sender as HyperlinkButton).Tag as Tbl_FailureTracking;
            faiClient.OverTrackingAsync(cuo.Id);
        }

        private void HyperlinkButton_Click_2(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(MyStrings.qdyscm, MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            var cuo = (sender as HyperlinkButton).Tag as Tbl_FailureTracking;
            faiClient.DeleteTrackingAsync(cuo.Id);
        }
    }

    public class FRTManagerControlCondition
    {
        public string PartName { get; set; }
        public string FailureName { get; set; }
    }
}
